#include <cstdio>
#include <cstring>

struct tree_node {
    tree_node *left_child;
    tree_node *right_child;
    int data; //for building sort_tree
} binary_tree[110];

int assigned_num;

tree_node *creat_node() {
    binary_tree[assigned_num].left_child = binary_tree[assigned_num].right_child = nullptr;
    return &binary_tree[assigned_num++];
}

char traverse_result_1[25], traverse_result_2[25];
int result_size_1, result_size_2;
char *current_string;
int *currint_string_size;

void post_order(tree_node *node) {
    if (node->left_child != nullptr)
        post_order(node->left_child);
    if (node->right_child != nullptr)
        post_order(node->right_child);
    current_string[(*currint_string_size)++] = char(node->data + '0');
}

void mid_order(tree_node *node) {
    if (node->left_child != nullptr)
        mid_order(node->left_child);
    current_string[(*currint_string_size)++] = char(node->data + '0');
    if (node->right_child != nullptr)
        mid_order(node->right_child);
}

tree_node *insert_node(tree_node *node, int num) {
    if (node == nullptr) {
        node = creat_node();
        node->data = num;
    } else if (num < node->data) {
        node->left_child = insert_node(node->left_child, num);
    } else if (num > node->data) {
        node->right_child = insert_node(node->right_child, num);
    }
    return node;
}

int main(int argc, char const *argv[]) {
    int n;
    char input_string[12];
    while (scanf("%d", &n) != EOF && n != 0) {
        assigned_num = 0;
        tree_node *root_node = nullptr;
        scanf("%s", input_string);
        for (int i = 0; input_string[i] != '\0'; ++i)
            root_node = insert_node(root_node, input_string[i] - '0');
        result_size_1 = 0;
        current_string = traverse_result_1;
        currint_string_size = &result_size_1;
        post_order(root_node);
        mid_order(root_node);
        traverse_result_1[result_size_1] = '\0';
        while (n-- != 0) {
            tree_node *root_node_2 = nullptr;
            scanf("%s", input_string);
            for (int i = 0; input_string[i] != '\0'; ++i)
                root_node_2 = insert_node(root_node_2, input_string[i] - '0');
            result_size_2 = 0;
            current_string = traverse_result_2;
            currint_string_size = &result_size_2;
            post_order(root_node_2);
            mid_order(root_node_2);
            traverse_result_2[result_size_2] = '\0';
            puts(strcmp(traverse_result_1, traverse_result_2) == 0 ? "YES" : "NO");
        }
    }
    return 0;
}